package com.scalar.chiptrack.operations.dataentry.lotsplit.actioncommands;

import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.scalar.chiptrack.operations.dataentry.LotDetails;
import com.scalar.chiptrack.operations.dataentry.dao.DataEntryDAO;
import com.scalar.chiptrack.operations.dataentry.lotsplit.forms.LotSplitForm;
import com.scalar.chiptrack.operations.process.dao.NewProductDAO;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.users.Functionality;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.actioncommands.ActionCommand;

import java.util.HashMap;
import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: Sudhakar
 * Date: Oct 18, 2006
 * Time: 12:56:18 PM
 */
public class LotSplitLoadProcessTypesCommand extends ActionCommand
{

    private static final String ACTION_NAME = "/lotSplit.do";

    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws Exception
    {


        LotSplitForm lotSplitForm = (LotSplitForm) form;

        HashMap errors = new HashMap();
        HashMap info = new HashMap();
        String action = request.getParameter("action");

        request.setAttribute(INFO_MESSAGES, info);
        request.setAttribute(ERROR_MESSAGES, errors);

        HttpSession mSession = request.getSession();
        User user = (User) mSession.getAttribute(SessionKeys.USER_KEY);

        if (user == null)
        {
            mSession.setAttribute(SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME);
            return (mapping.findForward("login"));
        }
        else
        {
            if (user.isActionAllowed(Functionality.DATA_ENTRY, User.USER_ACTION_MODIFY))
            {
                request.setAttribute("showMessage", "FALSE");
            }
            else
            {
                request.setAttribute("showMessage", "TRUE");
            }
        }

        lotSplitForm.setProcessType("0");
        lotSplitForm.setProcessStepId("0");
        lotSplitForm.setLocationId("0");
        lotSplitForm.setLotId("0");
        lotSplitForm.setAllwipLotsList( null );

        if ( user.getVendorId() > 0 )
        {
            request.setAttribute("processTypeList", DataEntryDAO.loadExternalVendorProcessList( null, user.getVendorId() ) );
        }
        else
        {
            request.setAttribute("processTypeList", NewProductDAO.loadAllProcessesFromDB(null, false) );
        }

        request.setAttribute("action", action);

        return mapping.findForward("lotSplit");
    }
}
